import axios from 'axios';
import constants from '../constants/index';
import configs from './common/configs.js';
import { getTokenHeader } from '../../utils/utils';
import {message} from 'antd';
import Qs from 'qs';
const {GET_RECRUIT_LIST_SUCCESS,GET_RECRUIT_LIST_FAILURE} = constants;
const baseUrl = configs.baseUrl;

/**
 * 获取招新列表
 * @param {*} query 
 */
export function getRecruitList(query = '') {
    return async dispatch => {
        try {
            let headers = getTokenHeader({});
            const data = (await axios.get(`${baseUrl}enrollment.do?${Qs.stringify(query)}`, {
                headers: headers,
            })).data;
            if(data.code==="SUCCESS")
            {
                dispatch({
                    type: GET_RECRUIT_LIST_SUCCESS,
                    data: data.data,
                });
            }
            else{
                message.error(data.msg);
            }
        } catch (error) {
            message.error('招新列表获取失败, 请稍后再试');
            dispatch({
                type: GET_RECRUIT_LIST_FAILURE,
                error: new Error('招新列表获取失败, 请稍后再试'),
            })
        }
    };
}

/**
 * 添加招新信息
 * @param {*} query Object 招新信息对象
 */
export function addRecruit(query=''){
    return async dispatch=>{
        try{
            let headers=getTokenHeader({});
             
            axios({
                method: 'POST',
                url: `${baseUrl}enrollment.do`,
                data: JSON.stringify(query),
                headers: {
                    'Content-Type': 'application/json',
                    ...headers
                },
                dataType:'json',
            }).then((data)=>{
                if(data.data.code==="SUCCESS"){
                    message.success("添加成功");
                }else{
                    message.warning("添加失败");
                }
            })
    
        }catch(error){
            message.warning("添加失败");
        }
    }
}

/**
 * 删除
 * @param {*} query 要删除的信息的ids
 */
export function deleteRecruitList(query = '') {
    return async dispatch => {
        try {
            let headers = getTokenHeader({});
            const data = (await axios.delete(`${baseUrl}enrollment.do?${Qs.stringify(query)}`, {
                headers: headers,
            })).data;
        } catch (error) {
            message.error('删除, 请稍后再试');
        }
    };
}



/**
 * 修改招新信息
 * @param {*} query Object 招新信息对象
 */
export function changeRecruit(query=''){
    return async dispatch=>{
        try{
            let headers=getTokenHeader({});
            
            axios({
                method: 'PUT',
                url: `${baseUrl}enrollment.do`,
                data: JSON.stringify(query),
                headers: {
                    'Content-Type': 'application/json',
                    ...headers
                    },
                dataType:'json',
            },{
                headers:headers
            }).then((data)=>{
                if(data.data.code==="SUCCESS"){
                    message.success("修改成功");
                }else{
                    message.warning("修改失败");
                }
            })
    
        }catch(error){
            message.warning("修改失败");
        }
    }
}